package top.deepdesigner.controller.systemManage;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsStorage;
import top.deepdesigner.service.DtsStorageService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 10:54:14 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/storage")
public class StorageController {
  @Autowired
  private DtsStorageService storageService;
  
  @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "添加")
  @RequiresPermissions(value = {"admin:os:create"})
  @PostMapping("/create")
  public Object create(MultipartFile file) throws Exception {
    DtsStorage dtsStorage = storageService.uploadImageFile(file);
    //上传文件并保存文件相关信息
    return ResponseUtil.ok(dtsStorage);
  }



  @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:os:list"})
  @GetMapping("/list")
  public Object list(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "20") Integer limit,
                     @RequestParam(defaultValue = "add_time") String sort,
                     @RequestParam(defaultValue = "desc") String order,
                     @RequestParam(defaultValue = "") String key,
                     @RequestParam(defaultValue = "") String name) {
    CommonListVO<DtsStorage> storageListVO = new CommonListVO<>();
    //获取分页数量
    Long count = storageService.getStorageCount();
    List<DtsStorage> dtsStorages = storageService.getStorageList(page, limit, sort, order, key,name);
    storageListVO.setTotal(count);
    storageListVO.setItems(dtsStorages);
    return ResponseUtil.ok(storageListVO);
  }
  
  @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "修改")
  @RequiresPermissions(value = {"admin:os:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsStorage os) {
    DtsStorage dtsStorage = storageService.updateStorage(os);
    return ResponseUtil.ok(dtsStorage);
  }

  @RequiresPermissionsDesc(menu = {"系统管理", "对象存储"}, button = "删除")
  @RequiresPermissions(value = {"admin:os:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsStorage os) {
    storageService.deleteStorage(os);
    return ResponseUtil.ok();
  }
}
